<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="计划id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coursePlanId">
              <a-select v-model="model.coursePlanId" placeholder="请选择教学计划"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in planList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="科目编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subjectId">
              <a-select v-model="model.subjectId" placeholder="请选择教学计划"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in subjectList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单双周标识" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courPlanAtomIzSingle">
              <a-input-number v-model="model.courPlanAtomIzSingle" placeholder="请输入单双周标识" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="周课时" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courPlanAtomWeekHours">
              <a-input-number v-model="model.courPlanAtomWeekHours" placeholder="请输入周课时" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学期课时" :labelCol="labelCol" :wrapperCol="wrapperCol"
                               prop="courPlanAtomSemestarHours">
              <a-input-number v-model="model.courPlanAtomSemestarHours" placeholder="请输入学期课时" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始周次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courPlanAtomStartWeek">
              <a-input-number v-model="model.courPlanAtomStartWeek" placeholder="请输入开始周次" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束周次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courPlanAtomEndWeek">
              <a-input-number v-model="model.courPlanAtomEndWeek" placeholder="请输入结束周次" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courPlanAtomStartDate">
              <j-date placeholder="请选择开始日期" v-model="model.courPlanAtomStartDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courPlanAtomEndDate">
              <j-date placeholder="请选择结束日期" v-model="model.courPlanAtomEndDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="courPlanAtomStatus">
              <j-dict-select-tag type="list" v-model="model.courPlanAtomStatus" dictCode="	cour_plan_atom_status"
                                 placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { getSubjectList, getCoursePlanDataList } from '@/api/commonAPI'

export default {
  name: 'CouCoursePlanAtomForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        courPlanAtomIzSingle: [
          { required: false },
          { pattern: /^-?\d+$/, message: '请输入整数!' }
        ],
        courPlanAtomWeekHours: [
          { required: false },
          { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
        ],
        courPlanAtomSemestarHours: [
          { required: false },
          { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }
        ],
        courPlanAtomStartWeek: [
          { required: false },
          { pattern: /^-?\d+$/, message: '请输入整数!' }
        ],
        courPlanAtomEndWeek: [
          { required: false },
          { pattern: /^-?\d+$/, message: '请输入整数!' }
        ]
      },
      url: {
        add: '/courseplanatom/couCoursePlanAtom/add',
        edit: '/courseplanatom/couCoursePlanAtom/edit',
        queryById: '/courseplanatom/couCoursePlanAtom/queryById'
      },
      planList: [],
      subjectList: []
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.initListData()
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      if (localStorage.length > 0) {
        let localData = JSON.parse(localStorage.getItem('pro__Login_Userinfo'))
        let tenantId = localData.value.tenantId
        this.model.tenantId = tenantId
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    async initListData() {
      let subjectRes = await getSubjectList()
      if (subjectRes.success) {
        this.subjectList = []
        subjectRes.result.forEach(item => {
          this.subjectList.push({
            label: item.subjectName,
            value: item.subjectId
          })
        })
      }

      let planRes = await getCoursePlanDataList()
      if (planRes.success) {
        this.planRes = []
        planRes.result.forEach(item => {
          this.planList.push({
            label: item.coursePlanName,
            value: item.id
          })
        })
      }
    }
  }
}
</script>